﻿using H.Eyes;
using H.Types;
using System;
using System.Collections.Generic;
using System.Linq;

namespace H.DML.See
{
    public class DMColorSee : ISee
    {
        /// <summary>
        /// 大漠实例
        /// </summary>
        protected readonly DMEye _Dml;

        /// <summary>
        /// 内部存储查找参数
        /// </summary>
        protected readonly DMOption _Option;

        ///<inheritdoc/>
        public IEnumerable<string> ObjNames { get; }

        /// <summary>
        /// 颜色查找字符串
        /// </summary>
        private readonly string ColorInfos;

        /// <summary>
        /// 构造一个颜色找图实例
        /// </summary>
        /// <param name="dml"></param>
        /// <param name="colors"></param>
        /// <param name="option"></param>
        public DMColorSee(DMEye dml, RGB[] colors, DMOption option)
        {
            ObjNames = (from c in colors select c.StrRGB).ToArray();
            _Dml = dml;
            _Option = option;
            if (_Option.DeletaColor.ValRGB == 0)
            {
                ColorInfos = string.Join("|", ObjNames);
            }
            else
            {
                string deletaColor = _Option.DeletaColor.StrRGB;
                ColorInfos = string.Join("|", from c in ObjNames select $"{c}-{deletaColor}");
            }
        }

        ///<inheritdoc/>
        public bool See(Rect matchRect)
        {
            if (matchRect.Left >= matchRect.Right || matchRect.Top >= matchRect.Bottom)
            {
                return false;
            }
            return _Dml.FindColor(matchRect, ColorInfos, _Option?.Similarly ?? 1.0, _Option?.DirectionValue ?? 0, out _) == 1;
        }

        ///<inheritdoc/>
        public int See(Rect matchRect, out Pos matchPoint)
        {
            if (matchRect.Left >= matchRect.Right || matchRect.Top >= matchRect.Bottom)
            {
                matchPoint = Pos.Empty;
                return -1;
            }
            return _Dml.FindColor(matchRect, ColorInfos, _Option?.Similarly ?? 1.0, _Option?.DirectionValue ?? 0, out matchPoint);
        }

        ///<inheritdoc/>
        public int[] See(Rect matchRect, out Pos[] matchPoints)
        {
            if (matchRect.Left >= matchRect.Right || matchRect.Top >= matchRect.Bottom)
            {
                matchPoints = null;
                return null;
            }
            string result = _Dml.FindColorEx(matchRect, ColorInfos, _Option?.Similarly ?? 1.0, _Option?.DirectionValue ?? 0);
            if (result.Length > 0)
            {
                string[] pieces = result.Split(',', '|');
                int count = pieces.Length / 2;
                int[] ret = new int[count];
                matchPoints = new Pos[count];
                for (int i = 0; i < count; i++)
                {
                    matchPoints[i] = new Pos(int.Parse(pieces[i]), int.Parse(pieces[count + i]));
                }
                return ret;
            }
            else
            {
                matchPoints = null;
                return null;
            }
        }


        /// <summary>
        /// 释放资源
        /// </summary>
        /// <param name="disposing"></param>
        private void Dispose(bool disposing)
        {
            if (disposing) { }
        }

        ///<inheritdoc/>
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        /// <summary>
        /// 析构函数
        /// </summary>
        ~DMColorSee()
        {
            Dispose(false);
        }

    }
}
